package com.lxd.sunso.product.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


import com.lxd.sunso.common.utils.DateUnit;
import com.lxd.sunso.common.utils.PageUtils;
import com.lxd.sunso.common.utils.R;
import com.lxd.sunso.product.vo.CustomerVo;
import com.lxd.sunso.product.vo.MachineNameAndIdVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lxd.sunso.product.entity.CustomerEntity;
import com.lxd.sunso.product.service.CustomerService;


/**
 * @author lxd
 * @email sunlightcs@gmail.com
 * @date 2020-12-30 18:06:30
 */
@RestController
@RequestMapping("product/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:customer:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = customerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:customer:info")
    public R info(@PathVariable("id") Integer id) {
        CustomerEntity customer = customerService.getById(id);

        return R.ok().put("customer", customer);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:customer:save")
    public R save(@RequestBody CustomerEntity customer) {
        customer.setCreatetime(DateUnit.dateFormat(new Date()));
        boolean flag = customerService.saveCustomer(customer);
        if (flag)
            return R.ok();
        else
            return R.error(1, "顾客名已使用");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:customer:update")
    public R update(@RequestBody CustomerEntity customer) {
        customerService.updateById(customer);

        return R.ok();
    }

    /**
     * 删除顾客以及对应的用户以及设备
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:customer:delete")
    public R delete(@RequestBody Integer[] ids) {
        customerService.removeAll(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 搜索
     *
     * @param params
     * @return
     */
    @RequestMapping("/search")
    //@RequiresPermissions("product:customer:list")
    public R search(@RequestParam Map<String, Object> params) {
        PageUtils page = customerService.search(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取客户的名称和ID
     */
    @RequestMapping("/getCusNameAndId")
    public R search() {
        List<CustomerVo> page = customerService.getCusNameAndId();
        return R.ok().put("list", page);
    }

    /**
     * 根据客户的名称获取设备
     */
    @RequestMapping("/getmachineByCustomer/{customerName}")
    public R getmachineByCustomer(@PathVariable String customerName) {
        List<MachineNameAndIdVo> list = customerService.getmachineByCustomer(customerName);
        return R.ok().put("list", list);
    }
}
